package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhwyChargedata;
import com.ruoyi.system.service.IZhwyChargedataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收费数据生成Controller
 * 
 * @author ruoyi
 * @date 2024-05-09
 */
@RestController
@RequestMapping("/chargedata/chargedata")
public class ZhwyChargedataController extends BaseController
{
    @Autowired
    private IZhwyChargedataService zhwyChargedataService;

    /**
     * 查询收费数据生成列表
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyChargedata zhwyChargedata)
    {
        startPage();
        List<ZhwyChargedata> list = zhwyChargedataService.selectZhwyChargedataList(zhwyChargedata);
        return getDataTable(list);
    }

    /**
     * 导出收费数据生成列表
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:export')")
    @Log(title = "收费数据生成", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyChargedata zhwyChargedata)
    {
        List<ZhwyChargedata> list = zhwyChargedataService.selectZhwyChargedataList(zhwyChargedata);
        ExcelUtil<ZhwyChargedata> util = new ExcelUtil<ZhwyChargedata>(ZhwyChargedata.class);
        util.exportExcel(response, list, "收费数据生成数据");
    }

    /**
     * 获取收费数据生成详细信息
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyChargedataService.selectZhwyChargedataById(id));
    }

    /**
     * 新增收费数据生成
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:add')")
    @Log(title = "收费数据生成", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyChargedata zhwyChargedata)
    {
        return toAjax(zhwyChargedataService.insertZhwyChargedata(zhwyChargedata));
    }

    /**
     * 修改收费数据生成
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:edit')")
    @Log(title = "收费数据生成", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyChargedata zhwyChargedata)
    {
        return toAjax(zhwyChargedataService.updateZhwyChargedata(zhwyChargedata));
    }

    /**
     * 删除收费数据生成
     */
    @PreAuthorize("@ss.hasPermi('chargedata:chargedata:remove')")
    @Log(title = "收费数据生成", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhwyChargedataService.deleteZhwyChargedataByIds(ids));
    }




    /**
     * 获取首页条状统计图数据
     * @return --返回符合条件数据
     */
    //@Anonymous //不需要经过认证
    @GetMapping("/getData")
    public AjaxResult getData(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 当前日期加一个月
        LocalDate datePlusOneMonth = currentDate.plusMonths(1);
        //增加一个月后，再次减去六个月
        LocalDate dateSixMonthsAgo = datePlusOneMonth.minus(Period.ofMonths(6));
        // 增加月份的第一天
        LocalDate firstDayOfMonth = datePlusOneMonth.with(TemporalAdjusters.firstDayOfMonth());
        // 减去六个月的第一天
        LocalDate firstDayOfMonth2 = dateSixMonthsAgo.with(TemporalAdjusters.firstDayOfMonth());
        // 日期格式化
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String current = sdf.format(firstDayOfMonth);
        String beforeSix = sdf.format(firstDayOfMonth2);
        //获取符合的日期个数
        List<ZhwyChargedata> zhwyChargedata = zhwyChargedataService.queryZhwyDate(current, beforeSix);
        //创建格式化对象
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        //创建Map集合
        Map<String,Object> map = new TreeMap<>();
        List<String> time = new ArrayList<>();
        List<Object> money = new ArrayList<>();
        //为键赋值日期
        for (int i=0; i<6; i++){
            LocalDate localDate = firstDayOfMonth2.plusMonths(i);
            //月初
            LocalDate start = localDate.with(TemporalAdjusters.firstDayOfMonth());
            String start2 = sdf.format(start);
            time.add(start2.substring(0,7));
            //月末
            LocalDate end = localDate.with(TemporalAdjusters.lastDayOfMonth());
            String end2 = sdf.format(end);
            List<ZhwyChargedata> zhwyChargedata1 = zhwyChargedataService.queryZhwyMonthData(start2, end2);
            Double money1 = 0.00;
            for (int j=0; j<zhwyChargedata1.size(); j++){
                money1 += zhwyChargedata1.get(j).getMoney();
            }
            money.add(money1);
            //设置日期和数据
            map.put("time",time);
            map.put("money",money);
        }
//
//
        return AjaxResult.success(map);
    }
}
